/**
 * 
 */
package com.scpii.universal.pns.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import org.hibernate.Hibernate;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.dao.impl.BaseDaoImpl;
import com.scpii.universal.pns.dao.TagsDao;
import com.scpii.universal.pns.entity.Tags;

/**
 * genericWeb
 * 
 * @author acer 2012-7-5下午04:35:11
 */
@Repository
public class TagsDaoImpl extends BaseDaoImpl<Tags, Long> implements TagsDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Tags> getUsedTags(Long targetId) {
		String hql = "select t from Tags t,TagValues v where t.id=v.tagId and v.targetId=?";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0, targetId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tags> getAllTags(Long objectId) {
		String hql = "from Tags t where t.objectId=?";
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql).setParameter(0, objectId).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tags> getTagsAndUseNum(Long objectId) {
		ResourceBundle bundle = ResourceBundle.getBundle(GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault());
		String pnsSchema = bundle.getString("pns.schema");
		String sql = "select t.id,t.title,count(v.id) as count from "+pnsSchema+".tags t left join "+pnsSchema+".tag_values v on t.id=v.tag_id " +
					"where t.object_id=? group by t.id,t.title order by t.id desc";
		List<Map<String,Object>> results = getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).setParameter(0, objectId).list();
		List<Tags> data = new ArrayList<Tags>();
		Tags tag = null;
		for(int i=0;i<results.size();i++){
			tag = new Tags();
			tag.setId(Long.parseLong(results.get(i).get("ID").toString()));
			tag.setTitle(results.get(i).get("TITLE").toString());
			tag.setCount(Integer.parseInt(results.get(i).get("COUNT").toString()));
			data.add(tag);
		}
		return data;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String,String> getTagNamesByTargetId(String targetIds) {
		ResourceBundle bundle = ResourceBundle.getBundle(GenericConstants.GLOBAL_PROPERTIES_PATH, Locale.getDefault());
		String pnsSchema = bundle.getString("pns.schema");
		String sql = "select t1.target_id as id,wmsys.wm_concat(t2.title) as name from "+pnsSchema+".tag_values t1,"+pnsSchema+".tags t2 "
				+ "where t1.tag_id=t2.id and t1.target_id in("+targetIds+") group by t1.target_id";
		List<Map<String,Object>> results = getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createSQLQuery(sql).addScalar("ID", Hibernate.LONG).addScalar("NAME", Hibernate.STRING).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		Map<String,String> data = new HashMap<String,String>();
		for (Map<String, Object> map : results) {
			data.put(map.get("ID").toString(), map.get("NAME").toString());
		}
		return data;
	}

}
